package com.teamclub.erp.scontrollers

import javax.validation.Valid
import com.teamclub.erp.sforms.SignedManagerForm
import com.teamclub.erp.sservices.SignedManagerService
import com.teamclub.util.errors.ErrorCode
import com.teamclub.util.libs.{Eithers, F}
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.validation.BindingResult
import org.springframework.web.bind.annotation.{PostMapping, RequestBody, RequestMapping, RestController}


@RestController("com.teamclub.erp.scontrollers.SignedManagerController")
@RequestMapping(Array("/erp/signcontract"))
class SignedManagerController {
  val logger = LoggerFactory.getLogger(classOf[SignedManagerController])

  @Autowired
  var service: SignedManagerService = _

  @PostMapping(Array("list"))
  def list(@RequestBody form: SignedManagerForm) = {
    Eithers.toJson(service.list(form))
  }
  @PostMapping(Array("pagerList"))
  def pagerList(@RequestBody form: SignedManagerForm) = {
    Eithers.toJson(service.pagerList(form))
  }
  @PostMapping(Array("add"))
  def add(@RequestBody @Valid form: SignedManagerForm, bindingResult: BindingResult) = {
    if(bindingResult.hasErrors) {
      logger.error("{}", bindingResult)
      Eithers.toJson(F.Either.Right(ErrorCode.BIZ_FAILURE))
    } else {
      Eithers.toJson(service.add(form))
    }
  }
  @PostMapping(Array("signedAll"))
  def signedAll(@RequestBody @Valid form: SignedManagerForm, bindingResult: BindingResult) =  {
    if(bindingResult.hasErrors) {
      logger.error("{}", bindingResult)
      Eithers.toJson(F.Either.Right(ErrorCode.BIZ_FAILURE))
    }
    Eithers.toJson(service.signedAll(form))
  }
}
